<?php
/**
 * the entry to access oauth and api server
 * most method in this class only a wrapper to RenrenOauth and RenrenApi
 * 
 * @author tom.wang<tom.wang.sz@qq.com>
 */
class RenrenClient {
	private static $cfg;
	private $oauth;
	private $api;
	
	public function __construct($cfg = array()) {
		$this->initial($cfg);
	}
	
	/**
	 * get config
	 *
	 * @param mixed $item
	 * @return mixed : if $item = null return whole config, otherwise return the specific config
	 */
	public static function getCfg($item = null) {
		$cfg = null;
		if(null === $item) $cfg = self::$cfg;
		elseif(array_key_exists($item, self::$cfg)) $cfg = self::$cfg[$item];
		
		return $cfg;
	}
	
	/**
	 * get authorize url
	 * you can use the return url to redirect user to renren to authorize
	 *
	 * @return string : the authorize url
	 */
	public function getAuthorizeUrl() {
		return $this->oauth->getAuthorizeUrl();
	}
	
	/**
	 * get access token
	 *
	 * @param string $code
	 * @return string : access token
	 */
	public function getAccessToken($code, $type = 'normal') {
		return $this->oauth->getAccessToken($code, $type);
	}
	
	/**
	 * set session key
	 */
	public function setAccessToken($access_token) {
		$this->api->setAccessToken($access_token);
	}
	
	/**
	 * set call id
	 */
	public function setCallId($call_id) {
		$this->api->setCallId($call_id);
	}
	
	/**
	 * get api version
	 */
	public function getVersion() {
		return $this->api->getVersion();
	}
	
	/**
	 * the entry to call api
	 */
	public function api($params = array(), $gzip = false) {
		return $this->api->api($params, $gzip);
	}
	
	private function initial($cfg = array()) {
		// initial config
		self::$cfg = require FRAMEWORK_RENREN_ROOT . 'config.php';
		foreach($cfg as $key => $val) {
			if(array_key_exists($key, self::$cfg)) self::$cfg[$key] = $val;
		}
		
		// initial oauth and api
		$this->oauth = new RenrenOauth();
		$this->api = new RenrenApi();
	}
}
?>